<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.gxgyl.com All rights reserved.

// +----------------------------------------------------------------------
// | Author: Liuyu
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\api\model\shiyao;

use app\api\model\shiyao\Pi as PiModel;
use app\api\model\User as UserModel;
use app\api\service\User as UserService;
use app\common\model\shiyao\Center as CenterModel;
use app\common\model\shiyao\ProjectCenter as CenterProjectModel;
use cores\exception\BaseException;

/**
 * 模型
 * Class
 * @package app\common\model
 */
class Center extends CenterProjectModel {
	/**
	 * 隐藏字段
	 * @var array
	 */
	protected $hidden = [
		'is_delete',
		'store_id',
		'create_time',
		'update_time',
	];

	/**
	 * 获取收货地址列表
	 * @return \think\Collection
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\DbException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws BaseException
	 */
	public function getList(array $params = []) {
		$userId = UserService::getCurrentLoginUserId();
		$filter = [];
		$filter[] = ['project_id', '=', $params['projectId']];

		!empty($params['search']) && $filter[] = ['center_name', 'like', "%{$params['search']}%"];

		// 实例化新查询对象
		$query = $this->getNewQuery();

		return $query->where($filter)
			->where('is_delete', '=', 0)
			->select()->toArray();
	}

	/**
	 * 新增
	 * @param array $data
	 * @return mixed
	 * @throws BaseException
	 */
	public function add(array $data) {
		// 当前用户信息
		$user = UserService::getCurrentLoginUser(true);

		// 添加
		return $this->transaction(function () use ($user, $data) {

			$data = array_merge($data, [
				'pm_id' => $user['user_id'],
				'store_id' => self::$storeId,
			]);
			$this->save($data);
			//收录center

			$data['company_id'] = $user['company_id'];
			
			$CenterModel = new CenterModel;

			$CenterModel->add($data);

			$UserModel = new UserModel;

			//新增PI账号
			$UserModel->increased([$data['pi']], 1);

			//新增CRC账号
			$UserModel1 = new UserModel;
			$UserModel1->increased($data['crcList'], 2);

			// 新增PI
			$Pi = new PiModel;

			$Pi->increased((int) $this['id'], $data['project_id'], $data['pi']);

			//新增CRC
			Crc::increased((int) $this['id'], $data['project_id'], $data['crcList']);

			return true;
		});
	}

	/**
	 * 编辑收货地址
	 * @param array $data
	 * @return bool
	 * @throws BaseException
	 */
	public function edit(array $data) {

		// 更新收货地址
		return $this->save($data) !== false;
	}

	/**
	 * 删除
	 * @return bool
	 * @throws BaseException
	 */
	public function remove() {
		// 查询当前是否为默认地址
		$user = UserService::getCurrentLoginUser(true);
		// 清空默认地址
		if ($user['address_id'] == $this['address_id']) {
			UserModel::updateBase(['address_id' => 0], ['user_id' => $this['user_id']]);
		}
		// 标记为已删除
		return $this->save(['is_delete' => 1]);
	}

	/**
	 * 收货地址详情
	 * @param int $addressId
	 * @return UserAddress|array|null
	 * @throws BaseException
	 */
	public static function detail(int $CenterId) {
		$userId = UserService::getCurrentLoginUserId();
		$detail = self::get(['Center_id' => $CenterId]);
		if (empty($detail)) {
			throwError('未找到该项目');
		}
		return $detail;
	}
}
